package com.mux.util.email;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;


/**
 * 发送邮件工具类
 * @author devin.chen
 *
 */
public class SendEmailUtil {
	//发件人服务器
	private String hostName="smtp.163.com";
	//发件人	
	private String userName="";
	//密码
	private String password="";
	//发件人姓名
	private  String fromName = "错误信息";
	
	public SendEmailUtil(){
		
	}
	
	public SendEmailUtil(String hostName, String userName, String password, String fromName) {
		super();
		this.hostName = hostName;
		this.userName = userName;
		this.password = password;
		this.fromName = fromName;
	}




	/**
	 * 发送普通邮件
	 * @param toAddress  收件人邮箱  不可为空
	 * @param isAuthen   是否需要验证用户名和密码
	 * @param toName	  收件人人姓名，可为空
	 * @param msg		发送内容 ，不可为空
	 * @param subject	主题	不可为空
	 * @throws EmailException
	 */
	public void sendSimpleEmail(String[] toAddress,boolean isAuthen,String toName,String msg,String subject) throws EmailException{
	    SimpleEmail email = new SimpleEmail();
        email.setHostName(hostName);// 设置smtp服务器
        if(isAuthen){
        	email.setAuthentication(userName, password);// 设置授权信息
        }
        email.setCharset("utf-8");
        email.setFrom(userName, fromName, "utf-8");// 设置发件人信息
        
        for(int i=0;i<toAddress.length;i++){
        	email.addTo(toAddress[i]);// 设置收件人信息
        }
        
        email.setSubject(subject);// 设置主题
        
        msg = StringUtils.isBlank(msg)?"":msg;
        email.setMsg(msg);// 设置邮件内容
        email.send();// 发送邮件
        
	}


	
	

	public String getUserName() {
		return userName;
	}



	public void setUserName(String userName) {
		this.userName = userName;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	public String getHostName() {
		return hostName;
	}



	public void setHostName(String hostName) {
		this.hostName = hostName;
	}


	public String getFromName() {
		return fromName;
	}



	public void setFromName(String fromName) {
		this.fromName = fromName;
	}
	
	public static void main(String[] args) throws Exception {
		SendEmailUtil sendEmailUtil = new SendEmailUtil();
		String[] emails = {"snbz_wgh@163.com","676405893@qq.com"};
		sendEmailUtil.sendSimpleEmail(emails,true,null, "这是一封测试邮件","明天");
		System.out.println("send success");
		
	}
	
}
